<template>
  <!-- 表单 -->
  <el-form ref="ruleForm" label-width="120px" :model="ruleForm" :rules="rules">
    <el-form-item label="姓名" prop="username">
      <el-input v-model="ruleForm.username" style="width:50%" placeholder="请输入姓名" />
    </el-form-item>
    <el-form-item label="手机" prop="mobile">
      <el-input v-model="ruleForm.mobile" style="width:50%" placeholder="请输入手机号" />
    </el-form-item>
    <el-form-item label="入职时间" prop="timeOfEntry">
      <el-date-picker v-model="ruleForm.timeOfEntry" style="width:50%" placeholder="请选择入职时间" />
    </el-form-item>
    <el-form-item label="聘用形式" prop="formOfEmployment">
      <el-select v-model="ruleForm.formOfEmployment" placeholder="请选择聘用形式">
        <el-option
          v-for="item in hiretype"
          :key="item.id"
          :label="item.value"
          :value="item.id"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="工号" prop="workNumber">
      <el-input v-model="ruleForm.workNumber" style="width:50%" placeholder="请输入工号" />
    </el-form-item>
    <el-form-item label="部门" prop="departmentName">
      <el-input v-model="ruleForm.departmentName" style="width:50%" placeholder="请输入部门" @focus="getDepartmentList()" />
      <el-tree v-if="DepartmentList.length" default-expand-all :data="DepartmentList" :props="{label:'name'}" @node-click="handleNodeClick" />
    </el-form-item>
    <el-form-item label="转正时间" prop="correctionTime">
      <el-date-picker v-model="ruleForm.correctionTime" style="width:50%" placeholder="请选择转正时间" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="hsubmit">确定</el-button>
      <el-button @click="$emit('close')">取消</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import EmployeeEnum from '@/constant/employees'
import { addEmployee } from '@/api/employees'
export default {
  data() {
    return {
      ruleForm: {
        username: '',
        mobile: '',
        timeOfEntry: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        correctionTime: ''
      },
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: ['change', 'blur'] }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ],
        correctionTime: [
          { required: true, message: '请选择转正时间', trigger: 'blur' }
        ]
      },
      DepartmentList: [],
      hiretype: EmployeeEnum.hireType
    }
  },
  created() {
    // this.getDepartmentList()
  },
  methods: {
    // 完成添加部门操作
    handleNodeClick(data) {
    //   console.log(data)
      // 如果有子部门的话,不允许选择
      if (data.children.length) return
      // 把部门名称赋值给表单
      this.ruleForm.departmentName = data.name
      // 关闭的话,清空数据
      this.DepartmentList = []
    },
    hsubmit() {
    //   console.log('点击了')
      this.$refs.ruleForm.validate(async valid => {
        // console.log(valid)
        if (!valid) return
        // 添加员工
        const res = await addEmployee(this.ruleForm)
        // console.log(res)
        // 提醒用户
        this.$message.success(res.message)
        // 通知父组件关闭弹框
        this.$emit('success')
      })
    },
    // 获取部门列表
    async getDepartmentList() {
      // 如果有数据的话,就不要再加载了
      if (this.DepartmentList.length) return
      const res = await getDepartments()
      //   console.log(res)
      // 删除第一条数据
      res.data.depts.shift()
      // 转换为树形结构
      this.DepartmentList = tranListToTreeData(res.data.depts)
    }
  }
}
</script>
<style lang="scss" scoped>
.el-tree {
    width: 210px;
    user-select: none;
}
</style>
